package com.fxd.shopping.product.service.impl;

import com.fxd.core.util.PageUtils;
import com.fxd.core.util.Query;
import com.fxd.shopping.product.service.CategoryBrandRelationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.fxd.shopping.product.dao.BrandDao;
import com.fxd.model.product.BrandEntity;
import com.fxd.shopping.product.service.BrandService;
import org.springframework.transaction.annotation.Transactional;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (StringUtils.isNotBlank(key)) {
            wrapper.eq("brand_id", key).or().like("name", key);
        }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params), wrapper

        );

        return new PageUtils(page);
    }


    @Transactional
    @Override
    public void updateDetail(BrandEntity brand) {

        this.updateById(brand);
        //保证多余字段的数据一致
        if (StringUtils.isNotBlank(brand.getName())) {
            //同步管理表中的数据
            categoryBrandRelationService.updateBrand(brand.getBrandId(), brand.getName());
            //TODO 为了数据一致性，需要更新其他关联
        }
    }
}